package com.small.auth.service;



import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 小程序的登陆
 *
 * @author wangguochao
 */
@Service
@Slf4j
@AllArgsConstructor
public class WxWeChatService {

    private final IAuUserService iAuUserService;
//    private final WxRequest wxUtil;


    //小程序登录
    public UserDetails loadUserByJsCode(Map<String, String> param) {
//        String jsCode = param.get("jsCode");
//        Map<String, String> map;
//        AuUser auUser = null;
        String username = null;
//        if (!"the code is a mock one".equals(jsCode)) {
//            map = wxUtil.code2Session(jsCode);
//            String openId = map.get("openid");
//            String session_key = map.get("session_key");
//            auUser = iAuUserService.findByOpenId(openId);
//            if (auUser == null) {
//                auUser = new AuUser();
//                auUser.setOpenId(openId);
//                username = openId;
//            }else {
//                username = auUser.getUserName();
//            }
//
//            auUser.setSessionKey(session_key);
//            iAuUserService.saveOrUpdate(auUser);
//        }
        Set<GrantedAuthority> set = new HashSet<>();
        set.add(new SimpleGrantedAuthority("wx-xcx-openid"));
        return new User("", "", set);
//        return CustomUser.builder().password(username).username(username).openid(username).auUser(auUser).authorities(set).build();
    }
}
